import Cesium from '../Ces/Cesium'
import WeUniform from './WeUniform'
import WeDisplayShaders from './WeDisplayShaders';

const nodataValue = -999.0

class WeDisplayEffectContour {
    _weUniform
    constructor(options) {
        const floatUniform = {
            contourValue: options.contourValue?? nodataValue,
            contourValue2: options.contourValue2?? nodataValue,
            contourValue3: options.contourValue3?? nodataValue,
            contourValue4: options.contourValue4?? nodataValue,
            contourValue5: options.contourValue5?? nodataValue,
            contourWidth: options.contourWidth || 2.0,
            contourOnly: options.contourOnly || 0.0,
        };

        const vec4Uniform = {
            contourColor: Cesium.Color.AQUA,
            contourColor2: Cesium.Color.AQUAMARINE,
            contourColor3: Cesium.Color.DARKTURQUOISE,
            contourColor4: Cesium.Color.LIGHTBLUE,
            contourColor5: Cesium.Color.LIGHTSKYBLUE,
        }
        this._weUniform = new WeUniform({
            floatUniform,
            vec4Uniform,
            name: 'WeDisplayEffectContour'
        });
    }

    get weUniform() {
        return this._weUniform;
    }

    get contourValue() {
        return this._weUniform.Properties.contourValue;
    }

    set contourValue(value) {
        this._weUniform.Properties.contourValue = value
    }

    get contourValue2() {
        return this._weUniform.Properties.contourValue2;
    }

    set contourValue2(value) {
        this._weUniform.Properties.contourValue2 = value
    }

    get contourValue3() {
        return this._weUniform.Properties.contourValue3;
    }

    set contourValue3(value) {
        this._weUniform.Properties.contourValue3 = value
    }

    get contourValue4() {
        return this._weUniform.Properties.contourValue4;
    }

    set contourValue4(value) {
        this._weUniform.Properties.contourValue4 = value
    }

    get contourValue5() {
        return this._weUniform.Properties.contourValue5;
    }

    set contourValue5(value) {
        this._weUniform.Properties.contourValue5 = value
    }

    set contourColor(value) {
        this._weUniform.Properties.contourColor.x = value.red;
        this._weUniform.Properties.contourColor.y = value.green;
        this._weUniform.Properties.contourColor.z = value.blue;
        this._weUniform.Properties.contourColor.w = value.alpha;
    }

    set contourColor2(value) {
        this._weUniform.Properties.contourColor2.x = value.red;
        this._weUniform.Properties.contourColor2.y = value.green;
        this._weUniform.Properties.contourColor2.z = value.blue;
        this._weUniform.Properties.contourColor2.w = value.alpha;
    }

    set contourColor3(value) {
        this._weUniform.Properties.contourColor3.x = value.red;
        this._weUniform.Properties.contourColor3.y = value.green;
        this._weUniform.Properties.contourColor3.z = value.blue;
        this._weUniform.Properties.contourColor3.w = value.alpha;
    }

    set contourColor4(value) {
        this._weUniform.Properties.contourColor4.x = value.red;
        this._weUniform.Properties.contourColor4.y = value.green;
        this._weUniform.Properties.contourColor4.z = value.blue;
        this._weUniform.Properties.contourColor4.w = value.alpha;
    }

    set contourColor5(value) {
        this._weUniform.Properties.contourColor5.x = value.red;
        this._weUniform.Properties.contourColor5.y = value.green;
        this._weUniform.Properties.contourColor5.z = value.blue;
        this._weUniform.Properties.contourColor5.w = value.alpha;
    }

    get contourOnly() {
        return this._weUniform.Properties.contourOnly === 1.0;
    }

    set contourOnly(value) {
        this._weUniform.Properties.contourOnly = value ? 1.0 : 0.0
    }

    pushDefines(defines) {
        if (this.contourValue > nodataValue) {
            defines.push(WeDisplayShaders.WeDefineHasContour)
        }
    }

    get shaderSource() {
        return `
        void WeContour(float value,inout vec4 color){
            float contourWidth = Weget_contourWidth();
            float contourValue = Weget_contourValue();
            float contourValue2 = Weget_contourValue2();
            float contourValue3 = Weget_contourValue3();
            float contourValue4 = Weget_contourValue4();
            float contourValue5 = Weget_contourValue5();
            bool contourOnly = Weget_contourOnly() > 0.0;
            vec4 contourColor = Weget_contourColor();
            vec4 contourColor2 = Weget_contourColor2();
            vec4 contourColor3 = Weget_contourColor3();
            vec4 contourColor4 = Weget_contourColor4();
            vec4 contourColor5 = Weget_contourColor5();
            float distanceToContour = mod(value, 0.001);
            float dxc = abs(dFdx(value));
            float dyc = abs(dFdy(value));
            float dF = max(dxc, dyc) * czm_pixelRatio * contourWidth;
            if(distanceToContour < dF
                && value > contourValue
                && value < contourValue + dF){
                color.rgba = contourColor;
            }
            else if(contourValue2 > -999.0
                && distanceToContour < dF
                && value > contourValue2
                 && value < contourValue2 + dF){
                color.rgba = contourColor2;
            }
            else if(contourValue3 > -999.0
                && distanceToContour < dF
                && value > contourValue3
                && value < contourValue3 + dF){
                color.rgba = contourColor3;
            }
            else if(contourValue4 > -999.0
                && distanceToContour < dF
                && value > contourValue4
                && value < contourValue4 + dF){
                color.rgba = contourColor4;
            }
            else if(contourValue5 > -999.0
                && distanceToContour < dF
                && value > contourValue5
                && value < contourValue5 + dF){
                color.rgba = contourColor5;
            }
            else if(contourOnly){
                color.a = 0.0;
            }
        }
        `
    }
}

export default WeDisplayEffectContour
